var BaseData = require('./config/db');
db = new BaseData();
db.connection.connect();

var _getUser = function(username, password, callback) {
	
	var sql = 'SELECT count(*),`User_id` FROM `userinfo` where `User` = ? and `PassWord` = ?';

	connection.query(sql, [username, password], function(err, results) {
		if(!err) {
			var res = hasUser(results)
			callback(res);

		} else {
			callback(error());
		}
	});

	function hasUser(results) {
		if(results.length == 0) {
			return {
				"code": "3001",
				"msg": "账号或密码错误！"
			};
		} else {
			return results[0];
		}
	}

	function error() {
		return {
			err: 1,
			msg: "数据库出错"
		};
	}
}

var getUser = function(username, password, callback) {
	return _getUser(username, password, callback);
}